package org.zkdemo.actors;

import javax.annotation.Resource;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;

public class CoreFactoryBean implements FactoryBean<ActorRef>
{
    
    @Resource
    private ApplicationContext applicationContext;
    
    @Autowired
    private ActorSystem        actorSystem;
    
    private ActorRef           core;
    
    private int                sessionTimeout;
    private int                port;
    private String             host;
    
    public CoreFactoryBean()
    {
    }
    
    @Override
    public ActorRef getObject() throws Exception
    {
        if (core == null)
        {
            core = actorSystem.actorOf(props(applicationContext, sessionTimeout, port, host), "core");
        }
        
        return core;
    }
    
    @Override
    public Class<ActorRef> getObjectType()
    {
        return ActorRef.class;
    }
    
    @Override
    public boolean isSingleton()
    {
        return true;
    }
    
    protected static Props props(final ApplicationContext applicationContext, int sessionTimeout, int port, String host)
    {
        return Props.create(new CoreCreator(applicationContext, sessionTimeout, port, host));
    }
    
    public int getSessionTimeout()
    {
        return sessionTimeout;
    }
    
    public void setSessionTimeout(int sessionTimeout)
    {
        this.sessionTimeout = sessionTimeout;
    }
    
    public int getPort()
    {
        return port;
    }
    
    public void setPort(int port)
    {
        this.port = port;
    }
    
    public String getHost()
    {
        return host;
    }
    
    public void setHost(String host)
    {
        this.host = host;
    }
    
}
